package cn.com.darly.videoandroidpm.ui.presenter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;

import com.darly.common.ToastApp;
import com.darly.common.retrofit.FrameListener;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import cn.com.darly.videoandroidpm.R;
import cn.com.darly.videoandroidpm.base.BaseActivity;
import cn.com.darly.videoandroidpm.bean.BaseBean;
import cn.com.darly.videoandroidpm.bean.FriendInfo;
import cn.com.darly.videoandroidpm.bean.LoginInfo;
import cn.com.darly.videoandroidpm.ui.biz.AddFriendListener;
import cn.com.darly.videoandroidpm.widget.shapeloading.ShapeLoadingDialog;


/**
 * @author zhangyh2 s 上午10:57:39 TODO 控制器 页面动作操作
 */
public class AddFriendPresenter {

    private AddFriendListener userBiz;
    private AddFriendListener.AddFriendInterface main;

    private ShapeLoadingDialog loading;


    public AddFriendPresenter(AddFriendListener.AddFriendInterface main) {
        this.main = main;
        this.userBiz = new AddFriendListener();
        loading = new ShapeLoadingDialog.Builder((BaseActivity)main)
                .loadText("请求中...")
                .build();
    }


    /**
     * @param context
     * @param v       TODO点击事件在这里进行处理
     */
    public void onClickDown(Context context, View v) {
        switch (v.getId()) {
            case R.id.title_view_back_img:
                main.end();
                break;
            case R.id.id_add_friend_search:
                if (TextUtils.isEmpty(main.findFriendName())){
                    ToastApp.showToast("用户不能为空");
                }else {
                    searchFriend();
                }
                break;
            default:
                break;
        }
    }


    public void searchFriend(){
        Map<String, String> params = new LinkedHashMap<String, String>();
        params.put("username", main.findFriendName());
        userBiz.onStart(loading);
        main.setDisableClick();
        userBiz.onSearch(params, new FrameListener<List<LoginInfo>>() {
            @Override
            public void onSucces(List<LoginInfo> info) {
                main.setEnableClick();
                userBiz.onStop(loading);
                main.onSearchSuccess(info);
            }

            @Override
            public void onFaild(int code, String info) {
                main.setEnableClick();
                userBiz.onStop(loading);
                if (code == 0){
                    ToastApp.showToast("服务端更新，请更新应用版本！");
                }else {
                    main.onSearchFailed(info);
                }
            }
        });

    }

    public void applyChecker() {
        userBiz.onStart(loading);
        main.setDisableClick();
        userBiz.onApply(new FrameListener<List<FriendInfo>>() {
            @Override
            public void onSucces(List<FriendInfo> info) {
                main.setEnableClick();
                userBiz.onStop(loading);
                main.onApplySuccess(info);

            }

            @Override
            public void onFaild(int code, String info) {
                main.setEnableClick();
                userBiz.onStop(loading);
                if (code == 0) {
                    ToastApp.showToast("服务端更新，请更新应用版本！");
                } else {
                    main.onApplyFailed(info);
                }
            }
        });
    }

    public void addFriend(Map<String, String> params) {
        userBiz.onStart(loading);
        main.setDisableClick();
        userBiz.onAddFriend(params, new FrameListener<BaseBean<Object>>() {
            @Override
            public void onSucces(BaseBean<Object> info) {
                main.setEnableClick();
                userBiz.onStop(loading);
                main.onAddFriendSuccess(info.getData());
            }

            @Override
            public void onFaild(int code, String info) {
                main.setEnableClick();
                userBiz.onStop(loading);
                if (code == 0){
                    ToastApp.showToast("服务端更新，请更新应用版本！");
                }else {
                    main.onAddFriendFailed(info);
                }
            }
        });
    }
}